package com.yhyvr.console.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.StringUtil;
import com.lz.mybatis.jdbc.auto.dao.BoProxyMapper;
import com.lz.mybatis.jdbc.auto.dao.BoProxyMessageMapper;
import com.lz.mybatis.jdbc.auto.model.BoProxy;
import com.lz.mybatis.jdbc.auto.model.BoProxyMessage;
import com.lz.tool.ListUtils;
import com.lz.tool.UUIDTool;
import com.yhyvr.console.annotation.RepeatSubmit;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.lz.kit.LogKit;
import com.lz.tool.LzStringUtils;
import com.yhyvr.pojo.resp.console.BootStrapGridResp;
import com.yhyvr.console.model.login.LoginUserModel;
import com.lz.mybatis.jdbc.auto.dao.BoMessageMapper;
import com.lz.mybatis.jdbc.auto.model.BoMessage;
import com.lz.mybatis.jdbc.auto.model.BoMessageExample;
import com.yhyvr.pojo.resp.CommonResp;
import com.yhyvr.pojo.resp.HttpResult;
import com.yhyvr.pojo.req.console.BootStrapGridReq;
import com.yhyvr.console.controller.ControllerQueryTool;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * title:
 * </p>
 * <p>
 * description:
 * </p>
 *
 * @create 张钦
 * @update
 * @version 1.0
 * @date
 */
@Controller
public class BoMessageController{
	@Resource
	private BoMessageMapper boMessageMapper;
	@Resource
	private BoProxyMapper boProxyMapper;
	@Resource
	private BoProxyMessageMapper boProxyMessageMapper;
	@RequestMapping("/console/bo_message/add")
	@RepeatSubmit(isAdd = true)
	public String add(){
		return "console/boMessage/boMessage_form";
	}

	@RepeatSubmit(isAdd = true)
	@RequestMapping("/console/bo_message/edit")
	public String edit(ModelMap modelMap,String id){
			modelMap.put("boMessage", boMessageMapper.selectByPrimaryKey(id));
		return "console/boMessage/boMessage_form";
	}

	@RequestMapping("/console/bo_message/list")
	public String list(){
		return "console/boMessage/boMessage_table";
	}

	@RequestMapping("/console/bo_message/json")
	public @ResponseBody BootStrapGridResp json(BootStrapGridReq bootGridReq){
		Page page = PageHelper.offsetPage(bootGridReq.getOffset(), bootGridReq.getLimit());
    	if(bootGridReq.getSort()!=null) {
    		page.setOrderBy(LzStringUtils.chageStringUpCaseAnd_(bootGridReq.getSort()) + " " + bootGridReq.getOrder());
    	}
		BoMessageExample boMessageExample = new BoMessageExample();
        ControllerQueryTool.setBoMessageCriteria(bootGridReq.getQuery(),boMessageExample.createCriteria());
		List<BoMessage> boMessageList = boMessageMapper.selectByExample(boMessageExample);
		return new BootStrapGridResp(page.getTotal(),boMessageList);
	}

	@RepeatSubmit(isAdd = false)
	@RequestMapping("/console/bo_message/save")
	public @ResponseBody HttpResult save(String formObjectJson){
		try{
		BoMessage boMessage= JSON.parseObject(formObjectJson,BoMessage.class);
			String id=boMessage.getId();
		if (id!=null) {
			boMessageMapper.updateByPrimaryKeySelective(boMessage);
			return CommonResp.getSuccess();
		}else{
			boMessage.setCreateTime(new Date());
			boMessage.setId(UUIDTool.getUUID());
			boMessageMapper.insertSelective(boMessage);
			return CommonResp.getSuccess();
		}
		}catch(Exception e){
			LogKit.error(e.getMessage());
			return CommonResp.getError();
		}

	}

	@RequestMapping("/console/bo_message/delete")
	public @ResponseBody HttpResult delete(String ids){
		String[]idsa=ids.split(",");
		for (String id : idsa) {
		boMessageMapper.deleteByPrimaryKey(String.valueOf(id));
		}
		return CommonResp.getSuccess();
	}

	@RequestMapping("/console/bo_message/push")
	public @ResponseBody HttpResult pushMessage(String id){
		BoMessage boMessage = boMessageMapper.selectByPrimaryKey(id);
		List<BoProxy> boProxies = boProxyMapper.selectByExample(null);
		if(ListUtils.isNotBlank(boProxies)) {
			for (BoProxy boProxy : boProxies) {
				BoProxyMessage boProxyMessage = new BoProxyMessage();
				boProxyMessage.setCreateTime(new Date());
				boProxyMessage.setHasRead(0);
				boProxyMessage.setId(UUIDTool.getUUID());
				boProxyMessage.setMessageContent(boMessage.getMessageContent());
				boProxyMessage.setMessageTitle(boMessage.getMessageTitle());
				boProxyMessage.setMessageType(boMessage.getMessageType());
				boProxyMessage.setProxyId(boProxy.getBoProxyId());
				boProxyMessageMapper.insertSelective(boProxyMessage);
			}
		}
		return CommonResp.getSuccess();
	}
}
